package com.kuaifawu.lwnlawyerclient.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.kuaifawu.lwnlawyerclient.LWNNetwork.LWNNetworkCenter;
import com.kuaifawu.lwnlawyerclient.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

public class LWNResetMM extends Activity implements View.OnClickListener{

    @ViewInject(R.id.reset_back)
    private ImageButton reset_back;
    @ViewInject(R.id.reset_show)
    private ImageButton reset_show;
    @ViewInject(R.id.reset_mm)
    private AutoCompleteTextView atextView_mm;
    @ViewInject(R.id.reset_finish)
    private Button reset_finish;
    private String id="";

    private Boolean bool_isShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetmm);
        id=getIntent().getExtras().getString("id");
        ViewUtils.inject(this);
        initResetView();
    }

    private void initResetView() {
        reset_finish.setOnClickListener(this);
        reset_show.setOnClickListener(this);
        reset_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reset_finish:
                 String mm=atextView_mm.getText().toString();
                finishFindmm(mm);
                break;

            case R.id.reset_show:
                if (bool_isShow) {
                    reset_show.setImageDrawable(getResources().getDrawable(R.drawable.login_hidden));
                    atextView_mm.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    reset_show.setImageDrawable(getResources().getDrawable(R.drawable.login_show));
                    atextView_mm.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
                }
                bool_isShow = !bool_isShow;

                break;
            case R.id.reset_back:
                finish();
                break;
        }
    }


    private void  finishFindmm(String mm){
        final ProgressDialog dialog=ProgressDialog.show(this,"","重置中，请稍后");
        LWNNetworkCenter center=LWNNetworkCenter.getInstance();
        String  sendCodeUrl=center.finishMMSetting(mm, this, id);
        HttpUtils http=center.getUtils();
        http.send(HttpRequest.HttpMethod.GET,sendCodeUrl,new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                dialog.dismiss();
                try{
                    JSONObject jsonObject= new JSONObject(objectResponseInfo.result.toString());
                    String msg=jsonObject.getString("msg");
                    String code=jsonObject.getString("code");
                    Toast toast = Toast.makeText(getApplicationContext(),
                            msg, Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    LinearLayout toastView = (LinearLayout) toast.getView();
                    ImageView imageCodeProject = new ImageView(getApplicationContext());
                    switch (Integer.parseInt(code)){
                        case 10000:
                            imageCodeProject.setImageResource(R.drawable.success);
                            toastView.addView(imageCodeProject, 0);
                            toast.show();
                            Intent intent=new Intent();
                            setResult(110,intent);
                            finish();
                            break;

                        case 10002:
                            imageCodeProject.setImageResource(R.drawable.error);
                            toastView.addView(imageCodeProject, 0);
                            toast.show();
                            break;



                    }
                }catch (Exception e){
                    e.printStackTrace();
                    dialog.dismiss();
                    Toast  toast = Toast.makeText(getApplicationContext(),
                            "网络错误，稍后重试", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    LinearLayout toastView = (LinearLayout) toast.getView();
                    ImageView imageCodeProject = new ImageView(getApplicationContext());
                    imageCodeProject.setImageResource(R.drawable.error);
                    toastView.addView(imageCodeProject, 0);
                    toast.show();

                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                dialog.dismiss();
                Toast toast = Toast.makeText(getApplicationContext(),
                        "网络错误，稍后重试", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                LinearLayout toastView = (LinearLayout) toast.getView();
                ImageView imageCodeProject = new ImageView(getApplicationContext());
                imageCodeProject.setImageResource(R.drawable.error);
                toastView.addView(imageCodeProject, 0);
                toast.show();
            }
        });





    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}
